package com.yangyouli.unity_demo_server.util;

public class Page<P> {
	private P param;
	private int currentPage;
	private int totalDataNum;
	private int dataNumPerPage;
	private int currentPageFirstDataIndex;
	private int maxPage;
	
	public Page () {}
	
	public Page(int currentPage, int dataNumPerPage) {
		this.currentPage = currentPage;
		this.dataNumPerPage = dataNumPerPage;
	}
	
	public Page(P param, int currentPage, int dataNumPerPage) {
		this.param = param;
		this.currentPage = currentPage;
		this.dataNumPerPage = dataNumPerPage;
	}

	/**
	 * 得到当前页数的第一个元素的索引，currentPage 必须被赋值
	 * @return
	 */
	public int getCurrentPageFirstDataIndex() {
		currentPageFirstDataIndex = (currentPage-1) * dataNumPerPage;
		return currentPageFirstDataIndex;
	}
	
	/**
	 * 得到最大页数，要求 totalDataNum dataNumPerPage 被赋予了值
	 * @return
	 */
	public int getMaxPage() {
		if (totalDataNum <= dataNumPerPage) {
			// 总记录数小于等于每一页容纳的记录数
			maxPage = 1;
		} else if (totalDataNum % dataNumPerPage > 0) {
			// 总记录数模上每一页容纳的记录数量大于0
			maxPage = totalDataNum / dataNumPerPage + 1;
		} else {
			// 总记录数模上每一页容纳的记录数量等于0
			maxPage = totalDataNum / dataNumPerPage;
		}
		return maxPage;
	}
	
	/**
	 * 当前页数加1，如果为最大页码则不变
	 */
	public void nextPage() {
		if(currentPage < maxPage) {
			currentPage++;
		}
	}
	
	/**
	 * 当前页数减1，如果为1则页码不变
	 */
	public void lastPage() {
		if(currentPage > 1) {
			currentPage--;
		}
	}

	public P getParam() {
		return param;
	}
	public void setParam(P param) {
		this.param = param;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getDataNumPerPage() {
		return dataNumPerPage;
	}
	public void setDataNumPerPage(int dataNumPerPage) {
		this.dataNumPerPage = dataNumPerPage;
	}
	public int getTotalDataNum() {
		return totalDataNum;
	}
	public void setTotalDataNum(int totalDataNum) {
		this.totalDataNum = totalDataNum;
	}
	@Override
	public String toString() {
		return "Page [param=" + param + ", currentPage=" + currentPage + ", totalDataNum=" + totalDataNum
				+ ", dataNumPerPage=" + dataNumPerPage + ", currentPageFirstDataIndex=" + currentPageFirstDataIndex
				+ ", maxPage=" + maxPage + "]";
	}
}
